import type { IQuery, IResponse, TableResponse } from '@/api/config/types';
import axios from 'axios';
import { downloadFileTemplate } from '../utils/files';
import type { LabeledValue } from 'ant-design-vue/es/select';

export interface MediaQueue {
  queueId: string;
  mediaId: string;
  resolutionId: string;
  goodsId: string;
  goodsName: string;
  barCode: string;
  retailPrice: null;
  memberPrice: null;
  salePrice: null;
  sendTime: string;
  doneTime: null;
  retryTimes: null;
  status: string;
  triggerType: string;
  deptId: string;
  nlast: number;
}
export interface Media {
  mediaId: string;
  mac: string;
  status: string;
  location: string;
  uri: null;
  refreshTimes: null;
  lastUpdateTime: null;
  lastOnlineTime: null;
  templateId: null;
  resolutionId: string;
  deptId: string;
  fwVersion: string;
  storeName: null;
  goodsName: null;
  share: null;
  barcode: null;
  templateName: null;
  resolutionDesc: null;
  resolutionWidth: number;
  resolutionHeight: number;
  nlast: string;
  label: string | string[];
  goodsList: any[];
  display: '0' | '1';
  conn: '0' | '1';
}
interface GoodsList {
  label: string;
  value: string;
}

type BoundData = {
  themeId: string;
  mediaId: string;
  goodsList?: string[];
};
// Base URL for the API
const prefix = '/media/device';

export const mediaDeviceList = (query: IQuery) => {
  return axios.get<TableResponse<Media>>(`/media/device/list`, { params: query });
};

export const fetchMediaList = (query: IQuery) => {
  const q = { ...query };
  if (q.mType) {
    q.mType = q.mType.join(',');
  }
  return axios.get(`${prefix}/list`, { params: q });
};

export const createMedia = (data: Media) => {
  return axios.post(prefix, data);
};

export const updateMedia = (data: Media) => {
  return axios.put(prefix, data);
};

export const getMediaById = (id: string | number) => axios.get<IResponse<Media>>(`${prefix}/${id}`);

export const deleteMedia = (ids: string[]) => {
  return axios.delete(`${prefix}/${ids.join(',')}`);
};

export const unboundMedia = (mediaId: string) => {
  return axios.post(`${prefix}/unbound`, { mediaId });
};

export const boundMedia = (data: BoundData) => {
  return axios.put(`${prefix}/bound`, data);
};
interface PreviewMediaConfig {
  templateId: string;
  goodsList: GoodsList[];
}
export const previewMedia = (data: PreviewMediaConfig) => {
  return axios.post(`${prefix}/preview`, data);
};

export const downloadManifest = () => {
  return axios.post(`${prefix}/manifest?scope=0`);
};

export const lcdManifest = () => {
  return downloadFileTemplate(`${prefix}/manifest?scope=0`);
};

export const lcdRelations = () => {
  return downloadFileTemplate(`${prefix}/relations?scope=0`);
};
type Firmware = {
  resolutionId: string;
  version: string;
};
type VersionType = {
  tags: string[];
  firmware: Firmware;
};
interface LabelGroup {
  label: string;
  mediaIds: string[];
}

export const firmwareUpgrade = (data: VersionType) => {
  return axios.post(`/firmware/firmware/rgb/upgrade`, data);
};

export const labelGroup = (data: LabelGroup) => {
  return axios.post(`${prefix}/label`, data);
};

export const fetchLabelOptions = (loadAll: boolean) =>
  axios.get<IResponse<LabeledValue[]>>(`${prefix}/label/options`, { params: { loadAll } });

export const automaticGrouping = () => {
  return axios.get(`${prefix}/groups`);
};
type DeviceConfig = {
  display?: '0' | '1';
  mediaIds: (string | number)[];
  conn?: '0' | '1';
};
export const deviceConfig = (config: DeviceConfig) => {
  return axios.post(`${prefix}/config`, config);
};
